% 评估个体适应度的逻辑,需要连接atk并创建对应的卫星
function Fitness_Matrix=GA_evaluateFitness(Population)
global conID    
%% 4.计算最大重访时间
%%% 多对多覆盖性分析设置
ind_num = size(Population,3);
Fitness_Matrix = zeros(ind_num,1);
% 创建字符串，与其他函数部分存在差异,为了代码的通用性牺牲速度
global Satellite_NeedNum
for ind_i = 1:1:ind_num
    Satellite_string = '';

    for i = 1:1:Satellite_NeedNum
        Satellite_string=[Satellite_string,' */Satellite/Satellite',num2str(ind_i),num2str(i),'/Sensor/Sensor',num2str(ind_i),num2str(i)];
    end
    atkConnect(conID, 'CovMulti',['/ Assets',Satellite_string]);

    Facility_Num = 20;
    Facility_string = '';

    for i = 1:1:Facility_Num
        Facility_string=[Facility_string,' */Facility/Target',num2str(i)];
    end
    Facility_string = [Facility_string,' */Ship/Ship'];
    atkConnect(conID, 'CovMulti',['/ objects',Facility_string]);
%atkConnect(conID, 'CovMulti', '/ Assets */Satellite/Satellite1/Sensor/Sensor1 */Satellite/Satellite2/Sensor/Sensor2 */Satellite/Satellite3/Sensor/Sensor3');% 选择卫星敏感器
%atkConnect(conID, 'CovMulti', '/ objects */Facility/Target1 */Facility/Target2 */Facility/Target3'); % 选择地面目标
    atkConnect(conID, 'CovMulti', '/ Access Compute "26 Sep 2035 12:00:00.00" "28 Sep 2035 12:00:00.00"'); % 计算指定时间段的覆盖性
    out = atkConnect(conID, 'CovMulti_RM', '/ multifomdefine Definition RevisitTime Compute Maximum'); % 计算最大重访时间品质值
%%% 从返回的out字符串中获取每个目标的最大重访时间
    time_str = regexp(out,'-?\d*\.?\d*','match');
    RevisitTime =[];
    %fprintf(['个体',num2str(i),'\n']);
    for i = 1:1:Facility_Num
        RevisitTime = [RevisitTime,str2double(time_str(3*i))];
        %fprintf(['Target',num2str(i),'MaxRevisitTime = %fh\n'],RevisitTime(i)/3600);
        if(RevisitTime(i) == -1)
            RevisitTime(i)=10000*3600;
        end
        %fprintf(['目标',num2str(i),'最大重访时间 = %fh\n'],RevisitTime(i)/3600);
        % 计算适应度
        if(RevisitTime(i)/3600 < 6)
            Fitness_Matrix(ind_i,1) = Fitness_Matrix(ind_i,1) + 4;
        else
            Fitness_Matrix(ind_i,1) = Fitness_Matrix(ind_i,1) + 4*exp(-(RevisitTime(i)/3600-6));
        end
    end
    % 垃圾操作，为了完成船的时间覆盖
    RevisitTime = [RevisitTime,str2double(time_str(3*Facility_Num+2))];
    if(RevisitTime(Facility_Num+1) == -1)
        RevisitTime(Facility_Num+1)=10000*3600;
    end
    if(RevisitTime(Facility_Num+1)/3600 < 3)
        Fitness_Matrix(ind_i,1) = Fitness_Matrix(ind_i,1) + 20;
    else
        Fitness_Matrix(ind_i,1) = Fitness_Matrix(ind_i,1) + 20*exp(-(RevisitTime(Facility_Num+1)/3600-3));
    end
    %输出个体适应度
    fprintf(['个体',num2str(ind_i),'适应度:%f   '],Fitness_Matrix(ind_i,1));
    if mod(ind_i,5) == 0
        fprintf('\n');
    end

end

%%% 输出每个目标的最大重访时间（-1代表该目标从未被重访）
% fprintf('Target1MaxRevisitTime = %f\n',RevisitTime(1));
% fprintf('Target2MaxRevisitTime = %f\n',RevisitTime(2));
% fprintf('Target3MaxRevisitTime = %f\n',RevisitTime(3));
end